package com.ymatou.iapi.hunter.testcase;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.LoginBindingRelationRequest;
import com.ymatou.iapi.hunter.service.LoginBindingRelationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterFans;
import com.ymttest.database.model.hunter.HunterRelation;
import com.ymttest.database.sqlwapper.hunter.HunterFansWrapper;
import com.ymttest.database.sqlwapper.hunter.HunterRelationWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_LoginBindingRelation {

	private static LoginBindingRelationRequest loginBindingRelationRequest;
	private static LoginBindingRelationCall loginBindingRelationCall;
	HunterRelationWrapper wrapper = new HunterRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("登录时通过邀请码绑定永久关系");
	}

	@Before
	public void caseUp() {
		loginBindingRelationRequest = new LoginBindingRelationRequest();
		loginBindingRelationCall = new LoginBindingRelationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(loginBindingRelationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb(String code, JSONObject o) throws JSONException {

		HunterRelationWrapper wrapper = new HunterRelationWrapper();
		HunterFansWrapper hunterFansWrapper = new HunterFansWrapper();

		HunterRelation actualHunterRelation = wrapper
				.selectHunterInfoByCode(code);
		HunterFans fans = hunterFansWrapper.selectFansByHunterIdAndUserId(
				actualHunterRelation.getHunter_id(),
				loginBindingRelationRequest.getUserId());

		Logger.verifyEquals(o.getLong("hunterId"),
				actualHunterRelation.getHunter_id(), "验证hunterId");
		Logger.verifyEquals(o.getString("hunterName"),
				actualHunterRelation.getHunter_name(), "验证hunterName");
		Logger.verifyEquals(loginBindingRelationRequest.getUserName(),
				fans.getUser_name(), "验证成为了粉丝");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_001() {
		Logger.start(true, "通过邀请码绑定成功");
		try {

			loginBindingRelationRequest.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			loginBindingRelationRequest.setUserName("autotest"
					+ TestDataManager.getRandomNum(10));
			loginBindingRelationRequest.setCode("KHL4YJ");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("success", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(true, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

			verifyDb(loginBindingRelationRequest.getCode(),
					loginBindingRelationCall.getResult());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_002() {
		Logger.start(true, "邀请码不存在");
		try {

			loginBindingRelationRequest.setUserId(Long.valueOf(TestDataManager
					.getRandomNum(10)));
			loginBindingRelationRequest.setUserName("autotest"
					+ TestDataManager.getRandomNum(10));
			loginBindingRelationRequest.setCode("mmmm");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("1001", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_003() {
		Logger.start(true, "用户已经是推手了");
		try {

			loginBindingRelationRequest.setUserId(4321);
			loginBindingRelationRequest.setUserName("autotest1");
			loginBindingRelationRequest.setCode("KHL4YJ");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("1002", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_004() {
		Logger.start(true, "用户已经绑定永久关系");
		try {

			loginBindingRelationRequest.setUserId(122222);
			loginBindingRelationRequest.setUserName("gds");
			loginBindingRelationRequest.setCode("KHL4YJ");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("1003", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_005() {
		Logger.start(true, "用户名不能为空");
		try {

			loginBindingRelationRequest.setUserId(122222);
			loginBindingRelationRequest.setUserName("");
			loginBindingRelationRequest.setCode("KHL4YJ");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("userName:用户名称不能为空", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_006() {
		Logger.start(true, "用户名不能为空");
		try {

			loginBindingRelationRequest.setUserId(0);
			loginBindingRelationRequest.setUserName("gds");
			loginBindingRelationRequest.setCode("KHL4YJ");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("userId:用户编号不能为空", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBindingRelation_007() {
		Logger.start(true, "邀请码不能为空");
		try {

			loginBindingRelationRequest.setUserId(122222);
			loginBindingRelationRequest.setUserName("gds");
			loginBindingRelationRequest.setCode("");

			loginBindingRelationCall.setData(loginBindingRelationRequest);
			loginBindingRelationCall.callService();

			Logger.verifyEquals("1006", loginBindingRelationCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(3, loginBindingRelationCall.getResponseData()
					.getInt("resultCode"), "验证resultCode");
			Logger.verifyEquals(false, loginBindingRelationCall
					.getResponseData().getBoolean("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
